/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog',
    'calendar'
], function ($, common, ready, template, Collection, Model, dialog) {

    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {

            loading();
            this.event();
            this.fetch();
        },

        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            var id = getUrlPath(3);
            this.collection = new Collection({

                url: _global.url.api + 'agency/child_list/' + id,
                condition: args,
                model: {
                    event: {},
                },
                callback: function () {
                    loaded();
                    $('.pagination').prepend('<li><a href="javascript:">所有小伙伴' + _this.collection.result.count.child + '人</a></li>');
                    $('.pagination').prepend('<li><a href="javascript:">所有分享家' + _this.collection.result.count.master + '人</a></li>');
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            $('.form_datetime').calendar();
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.time_style = $('select[name="time_style"] option:selected').val();
                _this.collection.condition.start_time = $('input[name="start_time"]').val();
                _this.collection.condition.end_time = $('input[name="end_time"]').val();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                _this.collection.reload();
            });

            $('.is-master').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.is_master = $(this).attr('rel');
                _this.collection.reload();
            })

        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});